package com.huoyun.announcement.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.huoyun.announcement.bean.Announcement;
import com.huoyun.announcement.service.AnnouncementService;
import com.huoyun.announcement.service.evt.GetOpenedAnnouncementPageEvt;
import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Page;

import com.huoyun.announcement.dao.evt.QueryAnnoucemnetPageEvt;

public class AnnouncementAction extends BaseAction {
	
	private static final Log log = LogFactory.getLog(AnnouncementAction.class);

	private AnnouncementService announcementService;
	
	private Announcement announcement;
	
	private Page annPage;
	
	private long annId;
	
	public long getAnnId() {
		return annId;
	}

	public void setAnnId(long annId) {
		this.annId = annId;
	}

	public String getOpenAnnouncement(){
		try {
			Date now=new Date();
			GetOpenedAnnouncementPageEvt evt=new GetOpenedAnnouncementPageEvt();
			evt.setPageCount(PAGE_COUNT);
			evt.setPageNo(pageNo);
			evt.setNowTime(now);
			annPage=announcementService.getOpenedAnnouncementPage(evt);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String announcementDetail(){
		try {
			announcement=announcementService.getAnnouncementById(annId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String preAdd()
	{
		return SUCCESS;
	}
	
	public String add()
	{
		announcement.setStatus(1);
		announcement.setAuthorId(getLoginUser().getId());
		announcement.setAuthorName(getLoginUser().getUsername());
		
		String strUserGroups = "";
		List<String> userGroups = announcement.getUserGroup();
		if(userGroups != null && userGroups.size() > 0)
		{
			for(String userGroup : userGroups)
			{
				strUserGroups +=  userGroup + ",";
			}
		}
		
		if(StringUtils.isNotEmpty(strUserGroups))
		{
			strUserGroups = strUserGroups.substring(0, strUserGroups.lastIndexOf(","));
		}
		announcement.setStrUserGroups(strUserGroups);
		
		try
		{
			announcementService.saveAnnoucement(announcement);
		}catch (Exception e) {
			log.error("添加公告失败！原因：" + e.fillInStackTrace());
			this.setResultMsg("系统忙，请稍后再试！");
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String preUpdate()
	{
		return SUCCESS;
	}
	
	public String queryAll()
	{
		return SUCCESS;
	}	
	
	public AnnouncementService getAnnouncementService() {
		return announcementService;
	}

	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}

	public Announcement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}

	public Page getAnnPage() {
		return annPage;
	}

	public void setAnnPage(Page annPage) {
		this.annPage = annPage;
	}

	
	
}
